//
//  SetPayPasswordViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/13.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "SetPayPasswordViewController.h"

@interface SetPayPasswordViewController ()

@property (strong, nonatomic) UIView *setPayPasswordView;
@property (strong, nonatomic) UILabel *setPayPasswordL;
@property (strong, nonatomic) UITextField *setPayPasswordTextField;
@property (strong, nonatomic) UIView *line1;

@property (strong, nonatomic) UIView *cPayPasswordView;
@property (strong, nonatomic) UILabel *cPayPasswordL;
@property (strong, nonatomic) UITextField *cPayPasswordTextField;
@property (strong, nonatomic) UIView *line2;

@property (strong, nonatomic) UIImageView *submitIV;
@property (strong, nonatomic) UILabel *submitL;

@end

@implementation SetPayPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WhiteColor;
    self.title = @"设置支付密码";
    
    [self createUI];
    
}
#pragma mark -提交点击事件
- (void)submitGesture:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"提交");
    
    if (self.setPayPasswordTextField.text.length <= 0) {
        
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入支付密码"];
        
        return ;
    }
    
    if (self.cPayPasswordTextField.text.length <= 0) {
        
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入确认支付密码"];
        return ;
        
    }
    
    NSString *payPassword = [RSA encryptString:[self.setPayPasswordTextField.text md5String] publicKey:PublicKey];
    NSString *cPayPassword = [RSA encryptString:[self.cPayPasswordTextField.text md5String] publicKey:PublicKey];
    
    NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", \"paypassword\" : \"%@\", \"repaypassword\" : \"%@\"}", [RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey], payPassword, cPayPassword];
    
    [PPNetworkHelper POST:@"user/Password/setpaypassword" parameters:@{@"json" : par}  success:^(id responseObject) {
        
        NSLog(@"设置支付密码%@",responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {

             [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
            
            [CommonUserInfo sharedInstance].is_setPayPassword = YES;
            
            if (self.controllerType == WithdrawType) {
                
                WithDrawViewController *vc = [[WithDrawViewController alloc] init];
                
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if (self.controllerType == SetPayPasswordType){
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }
            
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
            
        }
        
    }];
    
}

- (void)createUI {
    
    [self.view addSubview:self.setPayPasswordView];
    [self.setPayPasswordView addSubview:self.setPayPasswordL];
    [self.setPayPasswordView addSubview:self.setPayPasswordTextField];
    [self.setPayPasswordView addSubview:self.line1];
    
    [self.view addSubview:self.cPayPasswordView];
    [self.cPayPasswordView addSubview:self.cPayPasswordL];
    [self.cPayPasswordView addSubview:self.cPayPasswordTextField];
    [self.cPayPasswordView addSubview:self.line2];

    [self.view addSubview:self.submitIV];
    [self.view addSubview:self.submitL];
    
    __weak typeof(self) weakSelf = self;
    [self.setPayPasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.height.mas_equalTo(jisuan(92));
        
    }];
    [self.setPayPasswordL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.setPayPasswordView.mas_left).with.offset(jisuan(40));
        make.centerY.mas_equalTo(weakSelf.setPayPasswordView.mas_centerY);
        
        make.width.mas_equalTo(jisuan(154));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.setPayPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.setPayPasswordL.mas_right).with.offset(jisuan(40));
        make.centerY.mas_equalTo(weakSelf.setPayPasswordL.mas_centerY);
        make.right.mas_equalTo(weakSelf.setPayPasswordView.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.setPayPasswordView.mas_left).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.setPayPasswordView.mas_right).with.offset(-jisuan(40));
        make.bottom.mas_equalTo(weakSelf.setPayPasswordView.mas_bottom);
        make.height.mas_equalTo(jisuan(2));
    }];
    
    
    [self.cPayPasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.setPayPasswordView.mas_bottom);
        make.height.mas_equalTo(jisuan(92));
        
    }];
    [self.cPayPasswordL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.cPayPasswordView.mas_left).with.offset(jisuan(40));
        make.centerY.mas_equalTo(weakSelf.cPayPasswordView.mas_centerY);
        
        make.width.mas_equalTo(jisuan(154));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.cPayPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.setPayPasswordTextField.mas_left);
        make.centerY.mas_equalTo(weakSelf.cPayPasswordView.mas_centerY);
        make.right.mas_equalTo(weakSelf.cPayPasswordView.mas_right).with.offset(-jisuan(40));
        make.height.mas_equalTo(jisuan(34));
    }];
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.cPayPasswordView.mas_left).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.cPayPasswordView.mas_right).with.offset(-jisuan(40));
        make.bottom.mas_equalTo(weakSelf.cPayPasswordView.mas_bottom);
        make.height.mas_equalTo(jisuan(2));
    }];
    
    [self.submitIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom).with.offset(-jisuan(66));
        make.width.mas_equalTo(jisuan(550));
        make.height.mas_equalTo(jisuan(106));
    }];
    
    [self.submitL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.submitIV.mas_centerY).with.offset(-jisuan(4));
        make.centerX.mas_equalTo(weakSelf.submitIV.mas_centerX);
        
    }];
}



#pragma mark -lazy
- (UIView *)setPayPasswordView {
    
    if (!_setPayPasswordView) {
        
        _setPayPasswordView = [[UIView alloc] init];
        
    }
    
    return _setPayPasswordView;
    
}

- (UILabel *)setPayPasswordL {
    
    if (!_setPayPasswordL) {
        
        _setPayPasswordL = [[UILabel alloc] init];
        _setPayPasswordL.text = @"设置支付密码";
        _setPayPasswordL.textColor = Color333333;
        _setPayPasswordL.font = Font(24);
        _setPayPasswordL.textAlignment = NSTextAlignmentLeft;
        
    }
    
    return _setPayPasswordL;
}

- (UITextField *)setPayPasswordTextField {
    
    if (!_setPayPasswordTextField) {
        
        _setPayPasswordTextField = [[UITextField alloc] init];
        _setPayPasswordTextField.placeholder = @"请输入支付密码";
        _setPayPasswordTextField.textColor = Color333333;
        _setPayPasswordTextField.font = Font(24);
        _setPayPasswordTextField.textAlignment = NSTextAlignmentLeft;
        _setPayPasswordTextField.secureTextEntry = YES;
        
        _setPayPasswordTextField.keyboardType = UIKeyboardTypeDefault;
    }
    
    return _setPayPasswordTextField;
}

- (UIView *)line1 {
    
    if (!_line1) {
        
        _line1 = [[UIView alloc] init];
        _line1.backgroundColor = Colordddddd;
        
    }
    
    return _line1;
}

- (UIView *)cPayPasswordView {
    
    if (!_cPayPasswordView) {
        
        _cPayPasswordView = [[UIView alloc] init];
        
    }
    
    return _cPayPasswordView;
    
}

- (UILabel *)cPayPasswordL {
    
    if (!_cPayPasswordL) {
        
        _cPayPasswordL = [[UILabel alloc] init];
        _cPayPasswordL.text = @"确认支付密码";
        _cPayPasswordL.textColor = Color333333;
        _cPayPasswordL.font = Font(24);
        _cPayPasswordL.textAlignment = NSTextAlignmentLeft;
        
    }
    
    return _cPayPasswordL;
}

- (UITextField *)cPayPasswordTextField {
    
    if (!_cPayPasswordTextField) {
        
        _cPayPasswordTextField = [[UITextField alloc] init];
        _cPayPasswordTextField.placeholder = @"请输入确认支付密码";
        _cPayPasswordTextField.textColor = Color333333;
        _cPayPasswordTextField.font = Font(24);
        _cPayPasswordTextField.textAlignment = NSTextAlignmentLeft;
        _cPayPasswordTextField.secureTextEntry = YES;
        
        _cPayPasswordTextField.keyboardType = UIKeyboardTypeDefault;
    }
    
    return _cPayPasswordTextField;
}


- (UIView *)line2 {
    
    if (!_line2) {
        
        _line2 = [[UIView alloc] init];
        _line2.backgroundColor = Colordddddd;
        
    }
    
    return _line2;
}


- (UIImageView *)submitIV {
    
    if (!_submitIV) {
        
        _submitIV = [[UIImageView alloc] init];
        _submitIV.contentMode = UIViewContentModeScaleAspectFit;
        _submitIV.image = [UIImage imageNamed:@"redBtnBG"];
        _submitIV.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(submitGesture:)];
        [_submitIV addGestureRecognizer:gesture];
        
    }
    
    return _submitIV;
}
- (UILabel *)submitL {
    
    if (!_submitL) {
        
        _submitL = [[UILabel alloc] init];
        _submitL.text = @"提交";
        _submitL.textColor = WhiteColor;
        _submitL.textAlignment = NSTextAlignmentCenter;
        _submitL.font = Font(32);
        
    }
    
    return _submitL;
    
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
//    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
